

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.common.validator.ValidatorUtils;
import com.yz.dao.InclusionDao;
import com.yz.dao.RuinDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("inclusionService")
public class InclusionServiceImpl extends ServiceImpl<InclusionDao,InclusionEntity> implements InclusionService {
    @Autowired
    CheckService checkService;
    @Autowired
    InclusionDetailService inclusionDetailService;
    @Autowired
    ProjectService projectService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postInclusion(InclusionForm t) {
        InclusionEntity data = t.getInclusionEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(data.getUpdateAt());
        check.setCreateUser(data.getCreateUser());
        check.setName(projectService.selectById(data.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[10]);
        check.setProjectId(data.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(107);
        long id = 0;
        if(data.getId()==0){
            this.baseMapper.insert(data);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[10]+id);
            checkService.insert(check);
        }else{
            id = data.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[10]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[10]+id));
            inclusionDetailService.delete(new EntityWrapper<InclusionDetailEntity>().eq("inclusion_id",id));
            this.baseMapper.update(data,new EntityWrapper<InclusionEntity>().eq("id",id));
        }
        //  type:  A日记，B绘图，C拍照，D视频，E表单，F记录
//  type_form:如果是日记或者图形资料或者记录时：1勘探，2挖掘
//    如果是表单：1区域系统调查记录表,2遗址调查记录，
//  3断面观察记录，4钻探记录,101发掘记录表，102采样记录表,103全站测绘设置表，104全站测绘记录，表105出土器物标本登记表，
//  106陶器登记册，107包含物保存状况表,108墓葬发掘登记表
//        public static String[] type_all_form={"A","B","C","D","E","F"};
//        public static int[] type_detail_form={1,2,3,4,101,102,103,104,105,106,107,108,0};
//        public static String[] type_name_form={"区域系统调查记录表","遗址调查记录表","断面观察记录表","钻探记录表","发掘记录表","采样记录表","全站测绘设置表","全站测绘记录表","出土器物标本登记表","陶器登记册表","包含物保存状况表","墓葬发掘登记表"};
//        public static String[] type_name={"勘探日记","挖掘日记","勘探手绘图","挖掘手绘图","勘探照片","挖掘照片","勘探视频","挖掘视频","勘探记录","挖掘记录"};
//        public static String[] record_name={"探访记录","灰坑记录","房屋记录","陶窑记录","水井记录","壕沟记录","城址记录","其他记录"};
        List<InclusionDetailEntity> collections = t.getInclusionDetailEntities();
        for (int i = 0; i < collections.size(); i++) {
            InclusionDetailEntity item = collections.get(i);
            item.setInclusionId(id);
            inclusionDetailService.insert(item);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public InclusionForm getInclusionDetail(long id) {
        InclusionForm data = new InclusionForm();
        InclusionEntity t = new InclusionEntity();
        t.setId(id);
        t = this.baseMapper.selectById(t);
        List<InclusionDetailEntity> collections = this.baseMapper.selectInclusion(id);
        data.setInclusionEntity(t);
        data.setInclusionDetailEntities(collections);
        return data;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteInclusion(long id) {
        this.baseMapper.deleteById(id);
        checkService.deleteMe(5,107,id);
        inclusionDetailService.delete(new EntityWrapper<InclusionDetailEntity>().eq("inclusion_id",id));
    }
}
